package com.example.demo01.test;
 
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
 
@RestController
public class DemoReport1 {
    @Autowired
    private DataSource dataSource;
 
    @RequestMapping("/{reportName}")
    public void demoReport1(@PathVariable("reportName") final String reportName,
                            @RequestParam(required = false) Map<String, Object> parameters,
                            HttpServletResponse response, HttpServletRequest request) throws  Exception{
        parameters = parameters == null ? new HashMap<>(8) : parameters;
        //获取文件流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + "report.jasper");
        InputStream jasperStream = resource.getInputStream();

        parameters.put("hospitalName", "医院");
        parameters.put("patientName", "萨德好看萨达");
        parameters.put("patientSex", "a水果蛋糕)");
        parameters.put("patientAge", "s盛大官方的是");
        parameters.put("imageFindings", "report.getIma的撒供奉的是ngs()");
        parameters.put("diagnosticConclusion", "report.getDiag阿瑟东发多少个nosticConclusion()");
        //检查部位
        parameters.put("InspectionPosition", "");
 
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());



        //在线预览PDF
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
 
        //生成pdf文档
        /*String fileName = "F:/filename.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint,fileName);*/
 
        //生成word文档
       /* String fileName = "F:/filename.doc";
        JRRtfExporter docExporter = new JRRtfExporter();
        docExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,fileName);
        docExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        docExporter.exportReport();*/
 
        //生成excel文档
        /*JRXlsExporter excel = new JRXlsExporter();
        System.out.println(request.getServletContext().getRealPath("jaspers/demoreport1.jasper"));
        String fileName = "F://filename.xls";
        excel.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,fileName);
        excel.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        excel.exportReport();*/
 
    }
}